<?php

namespace app\admin\controller;

use app\BaseController;
use app\service\admin\AdminLogic;
use app\service\admin\AdminPurviewLogic;
use app\service\admin\AdminRoleLogic;
use think\facade\Session;

class Admin extends BaseController
{
    public function index()
    {
        $arrTemplate = [
            ['field' => 'id', 'title' => 'id'],
            ['field' => 'username', 'title' => '用户名'],
            ['field' => 'role_name', 'title' => '角色'],
            ['field' => 'real_name', 'title' => '真实姓名'],
            ['field' => 'mobile', 'title' => '手机号码'],
            ['field' => 'create_dt', 'title' => '创建时间'],
            ['field' => '', 'title' => '操作', 'width' => 200, 'toolbar'=> '#rowBar'],
        ];
        $template = json_encode($arrTemplate);
        $username = $this->request->param('username');
        $roleId = $this->request->param('role_id');
        $limit = $this->request->param('limit');
        if (!$this->request->isPost()) {
            return view('', compact('template', 'username', 'roleId', 'limit'));
        } else {
            $arrData = AdminLogic::getInstance()->getList($username, $roleId, $limit);
            return success('', $arrData);
        }
    }


    public function add()
    {
        if ($this->request->isPost()) {
            $arrParam = $this->request->all();
            // 字段校验
            try {
                validate(\app\validate\Admin::class)->check($arrParam);
            } catch (\Exception $e) {
                return error($e->getMessage());
            }

            try {
                AdminLogic::getInstance()->add(
                    $arrParam['username'],
                    $arrParam['password'],
                    $arrParam['re_password'],
                    $arrParam['real_name'],
                    $arrParam['mobile'],
                    $arrParam['role_id']
                );
            } catch (\Exception $e) {
                return error($e->getMessage());
            }
            return success('已添加');
        } else {
            $arrRole = AdminRoleLogic::getInstance()->getAll();
            return view('', compact('arrRole'));
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            $arrParam = $this->request->all();
            try {
                AdminLogic::getInstance()->edit(
                    $id,
                    $arrParam['username'],
                    $arrParam['password'],
                    $arrParam['re_password'],
                    $arrParam['real_name'],
                    $arrParam['mobile'],
                    $arrParam['role_id']
                );
            } catch (\Exception $e) {
                return error($e->getMessage());
            }
            return success('已更新');
        } else {
            $arrRole = AdminRoleLogic::getInstance()->getAll();
            $arrData = AdminLogic::getInstance()->getRowById($id);
            return view('', compact('arrRole', 'arrData'));
        }
    }

    public function del()
    {
        $id = $this->request->param('id');
        AdminLogic::getInstance()->del($id);
        return success('已删除');
    }

    /**
     * 修改密码
     */
    public function modifyPassword()
    {
        $id = Session::get('admin_id');
        if ($this->request->isPost()) {
            $password = $this->request->param('password');
            $rePassword = $this->request->param('re_password');
            if (empty($password) || ($password != $rePassword)) {
                return error('密码不能为空且重复密码需要一致');
            }
            AdminLogic::getInstance()->modifyPassword($id, $password);
            return success('修改成功');
        } else {
            return view('');
        }
    }
}